<?php
    
    class Catalog_model extends CI_Model{
        public function __construct()
        {
            $this->load->database();
            $this->load->library('session');
        }
        
        
        public function getAll()
        {
            $sql = "select * from catalog order by orders";
            $query = $this->db->query($sql);
         
            return $query->result_array();
        }

    
        public function get($parentid)
        {
            $sql = "select * from catalog where parentid=".$parentid." and enable=1 order by orders";
            $query = $this->db->query($sql);
         
            return $query->result_array();
        }

        public function getOne($id)
        {
              $sql = "select * from catalog where id=".$id;
              $query = $this->db->query($sql);
              $res  = $query->row();

            return $res;
        }

        public function setEnable($id, $enable){
            $data = array(
                'enable' => $enable
                );

           $this->db->where('id', $id);  
           $this->db->update('catalog', $data);
        }

        public function add()
        {
            $data = array('title' => $this->input->post('title'),
                    'orders' => $this->input->post('orders'),
                    'parentid' => $this->input->post('parentid'),
                    'enable' => 0
                    );

            $this->db->insert('catalog', $data);
        }
        
        public function update($id)
        {
            $data = array('title' => $this->input->post('title'),
                    'orders' => $this->input->post('orders'),
                    'parentid' => $this->input->post('parentid')
                    );
       
            $this->db->where('id', $id);  
            $this->db->update('catalog', $data);
        }
        
        
        public function delete($id)
        {
            $this->db->where('id', $id);  
            $this->db->delete('catalog');
        }
        

}